home *** CD-ROM | disk | FTP | other *** search
/ Isometric Game Programming with DirectX 7.0 / Isometric Game Programming.iso / source / chapter10 / isohex10_1 / wavloader.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-25  |  2.5 KB  |  139 lines

  1. // WAVLoader.cpp: implementation of the CWAVLoader class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4.  
  5. #include "WAVLoader.h"
  6.  
  7. //////////////////////////////////////////////////////////////////////
  8. // Construction/Destruction
  9. //////////////////////////////////////////////////////////////////////
  10.  
  11. CWAVLoader::CWAVLoader()
  12. {
  13.     //initialize members
  14.     lpWfx=NULL;
  15.     dwDataLength=0;
  16.     ucData=NULL;
  17. }
  18.  
  19. CWAVLoader::~CWAVLoader()
  20. {
  21.     //destroy any data we may have
  22.     Destroy();
  23. }
  24.  
  25. //get data length
  26. DWORD CWAVLoader::GetLength()
  27. {
  28.     return(dwDataLength);
  29. }
  30.  
  31. //get data pointer
  32. UCHAR* CWAVLoader::GetData()
  33. {
  34.     return(ucData);
  35. }
  36.  
  37. //get pointer to format
  38. LPWAVEFORMATEX CWAVLoader::GetFormat()
  39. {
  40.     return(lpWfx);
  41. }
  42.  
  43. //load from a file
  44. void CWAVLoader::Load(LPCTSTR lpszFilename)
  45. {
  46.     //destroy any old buffer
  47.     Destroy();
  48.  
  49.     //four character buffer
  50.     char Buffer[5];
  51.     Buffer[4]=0;
  52.  
  53.     //read length
  54.     DWORD dwNumRead;
  55.  
  56.     //length variable
  57.     DWORD dwLength;
  58.  
  59.     //data buffer
  60.     UCHAR* ucTemp;
  61.  
  62.     //open a handle to the file
  63.     HANDLE hfile=CreateFile(lpszFilename,GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
  64.  
  65.     ReadFile(hfile,Buffer,4,&dwNumRead,NULL);//"RIFF"
  66.  
  67.     ReadFile(hfile,&dwLength,sizeof(dwLength),&dwNumRead,NULL);//length of file
  68.  
  69.     ReadFile(hfile,Buffer,4,&dwNumRead,NULL);//"WAVE"
  70.  
  71.     //chunks
  72.     bool done=false;
  73.  
  74.     while(!done)
  75.     {
  76.         ReadFile(hfile,Buffer,4,&dwNumRead,NULL);//chunk header
  77.  
  78.         ReadFile(hfile,&dwLength,sizeof(dwLength),&dwNumRead,NULL);//length of chunk
  79.  
  80.         ucTemp=new UCHAR[dwLength];
  81.  
  82.         ReadFile(hfile,ucTemp,dwLength,&dwNumRead,NULL);
  83.  
  84.         //dependin on the chunk header, do something
  85.  
  86.         if(strcmp("fmt ",Buffer)==0)
  87.         {
  88.             //format chunk
  89.  
  90.             //allocate format
  91.             lpWfx=new WAVEFORMATEX;
  92.  
  93.             //clear out format
  94.             memset(lpWfx,0,sizeof(WAVEFORMATEX));
  95.     
  96.             //copy from buffer
  97.             memcpy(lpWfx,ucTemp,dwLength);
  98.         }
  99.  
  100.         if(strcmp("data",Buffer)==0)
  101.         {
  102.             //data chunk
  103.  
  104.             //allocate data buffer
  105.             ucData=new UCHAR[dwLength];
  106.  
  107.             //copy length
  108.             dwDataLength=dwLength;
  109.             
  110.             //copy buffer
  111.             memcpy(ucData,ucTemp,dwDataLength);
  112.  
  113.             //we are done, and need no more chunks
  114.             done=true;
  115.         }
  116.  
  117.         delete ucTemp;
  118.     }
  119.     //close the file
  120.     CloseHandle(hfile);
  121. }
  122.  
  123. //destroy buffer
  124. void CWAVLoader::Destroy()
  125. {
  126.     //if we have data, delete
  127.     if(ucData)
  128.     {
  129.         //delete format and data buffers
  130.         delete lpWfx;
  131.         delete ucData;
  132.  
  133.         //reinitialize
  134.         lpWfx=NULL;
  135.         ucData=NULL;
  136.         dwDataLength=NULL;
  137.     }
  138. }
  139.